home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / irit / iritinit.irt < prev    next >
Text File  |  1995-12-30  |  10KB  |  294 lines

  1. iritState("EchoSource", false);
  2. #
  3. # Init file for IRIT solid modeller.
  4. #
  5. iritstate( "FloatFrmt", "%8.6lg" );
  6.  
  7. demo = procedure():
  8.     include("demo.irt");
  9.  
  10. pause = procedure():t:
  11.     printf("Press return to continue:", nil()):
  12.     t = getline(string_type);
  13.  
  14. #
  15. # Some simple functions.
  16. #
  17. min = function(x, y):
  18.     if (x > y, return = y, return = x);
  19. max = function(x, y):
  20.     if (x > y, return = x, return = y);
  21. sqr = function(x):
  22.     return = x * x;
  23. normalize = function(x):len:
  24.     return = 1.0:
  25.     if (thisobj("x") == vector_type || thisobj("x") == point_type,
  26.     len = sqrt(x * x):
  27.     return = point(coord(x, 0) / len,
  28.                coord(x, 1) / len,
  29.                coord(x, 2) / len)):
  30.     if (thisobj("return") == numeric_type,
  31.     printf("NORMALIZE: Can handle only vectors or points, found %8.6lDg\n",
  32.            list(x)));
  33. midPoint = function(pt1, pt2):
  34.     return = (pt1 + pt2) * 0.5;
  35. InterpPoint = function(pt1, pt2, t):
  36.     return = (pt1 * (1 - t) + pt2 * t);
  37. edge2d = function( x1, y1, x2, y2 ):
  38.     return = ctlpt( E2, x1, y1 ) + ctlpt( E2, x2, y2 );
  39. edge3d = function( x1, y1, z1, x2, y2, z2 ):
  40.     return = ctlpt( E3, x1, y1, z1 ) + ctlpt( E3, x2, y2, z2 );
  41.  
  42. #
  43. # Sweep of circular cross section.
  44. #
  45. SwpCircSrf = function( AxisCrv, ScaleCrv, ScaleRefine ):
  46.     return = SwpSclSrf( circle( vector( 0, 0, 0 ), 1 ),
  47.             AxisCrv,
  48.             ScaleCrv,
  49.             off,
  50.             ScaleRefine );
  51.  
  52. #
  53. # Extractions of Control Polygon/Mesh/Points from a curve or a surface.
  54. #
  55. GetCtlPoints = function( Crv, Vecs ):i:p:
  56.     return = nil():
  57.     for ( i = 1, 1, sizeof( Crv ),
  58.     if ( Vecs,
  59.          p = coerce( coord( Crv, i - 1 ), vector_type ),
  60.          p = coerce( coord( Crv, i - 1 ), point_type ) ):
  61.     snoc( p, return )
  62.     );
  63. GetCtlPolygon = function( Crv ):
  64.     return = poly( GetCtlPoints( Crv, false ), true );
  65.  
  66. GetCtlMeshPts = function( Srf, Vecs ):l:i:j:p:rsize:csize:
  67.     return = nil():
  68.     rsize = meshsize( Srf, ROW ):
  69.     csize = meshsize( Srf, ROW ):
  70.     for ( i = 1, 1, rsize,
  71.         for ( j = 1, 1, csize,
  72.          if ( Vecs,
  73.               p = coerce( coord( Srf, (i - 1) * csize + j - 1 ),
  74.                   vector_type ),
  75.               p = coerce( coord( Srf, (i - 1) * csize + j - 1 ),
  76.                   point_type ) ):
  77.          snoc( p, return )
  78.     )
  79.     );
  80. GetCtlMesh = function( Srf ):l:i:j:p:pl:rsize:csize:first:
  81.     first = true:
  82.     rsize = meshsize( Srf, ROW ):
  83.     csize = meshsize( Srf, ROW ):
  84.     for ( i = 1, 1, rsize,
  85.     pl = nil():
  86.         for ( j = 1, 1, csize,
  87.          p = coerce( coord( Srf, (i - 1) * csize + j - 1 ), vector_type ):
  88.          snoc( p, pl )
  89.     ):
  90.     if ( first == true,
  91.          return = poly( pl, true ): first = false,
  92.          return = return + poly( pl, true ) )
  93.     ):
  94.     for ( j = 1, 1, csize,
  95.     pl = nil():
  96.         for ( i = 1, 1, rsize,
  97.          p = coerce( coord( Srf, (i - 1) * csize + j - 1 ), vector_type ):
  98.          snoc( p, pl )
  99.     ):
  100.     return = return + poly( pl, true )
  101.     );
  102.  
  103. #
  104. # Approximate a (assumed to be) closed planar curve as a polygon
  105. # with n vertices.
  106. #
  107. CnvrtCrvToPolygon = function( Crv, n ):ptl:pt:t:t0:t1:dt:
  108.     ptl = nil():
  109.     t0 = nth( pdomain( Crv ), 1 ):
  110.     t1 = nth( pdomain( Crv ), 2 ):
  111.     if ( n < 2, n = 2 ):
  112.     dt = (t1 - t0) / (n - 1):
  113.     for ( t = t0, dt, t1 - dt / 2,
  114.     pt = ceval( Crv, t ):
  115.     snoc( pt, ptl )
  116.     ):
  117.     return = poly( ptl, false );
  118.  
  119. #
  120. # Primitives in freeform surface form.
  121. #
  122. planeSrf = function( x1, y1, x2, y2 ):
  123.     return = ruledSrf( edge2d( x1, y1, x2, y1 ),
  124.                edge2d( x1, y2, x2, y2 ) );
  125. sphereSrf = function( Radius ):s45:
  126.     s45 = sin( 45 * pi / 180 ):
  127.     return = surfRev( cbspline( 3,
  128.                 list( ctlpt( P3, 1.0,  0.0, 0.0,  1.0 ),
  129.                       ctlpt( P3, s45, -s45, 0.0,  s45 ),
  130.                       ctlpt( P3, 1.0, -1.0, 0.0,  0.0 ),
  131.                       ctlpt( P3, s45, -s45, 0.0, -s45 ),
  132.                       ctlpt( P3, 1.0,  0.0, 0.0, -1.0 ) ),
  133.                 list( 0, 0, 0, 1, 1, 2, 2, 2 ) ) ) *
  134.     scale( vector( Radius, Radius, Radius ) );
  135. torusSrf = function( MRadius, mRad ):
  136.     return = SwpCircSrf( circle( vector( 0, 0, 0 ), MRadius ), mRad, 0 );
  137. coneSrf = function( Height, Radius ):
  138.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  139.               ctlpt( E3, Radius, 0.0, 0.0 ) +
  140.               ctlpt( E3, 0.0, 0.0, Height ) );
  141. cone2Srf = function( Height, Radius1, Radius2 ):
  142.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  143.               ctlpt( E3, Radius1, 0.0, 0.0 ) +
  144.               ctlpt( E3, Radius2, 0.0, Height ) +
  145.               ctlpt( E3, 0.0, 0.0, Height ) );
  146. cylinSrf = function( Height, Radius ):
  147.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  148.               ctlpt( E3, Radius, 0.0, 0.0 ) +
  149.               ctlpt( E3, Radius, 0.0, Height ) +
  150.               ctlpt( E3, 0.0, 0.0, Height ) );
  151. boxSrf = function( Width, Depth, Height ):
  152.     return = list( coerce( planeSrf( 0, 0, Width, Depth ), e3 ),
  153.            coerce( planeSrf( 0, 0, Width, Depth ), e3 ) *
  154.                trans( vector( 0, 0, Height ) ),
  155.            coerce( planeSrf( 0, 0, Width, Height ), e3 ) *
  156.                rotx( 90 ),
  157.            coerce( planeSrf( 0, 0, Width, Height ), e3 ) *
  158.                rotx( 90 ) * trans( vector( 0, Depth, 0 ) ),
  159.            coerce( planeSrf( 0, 0, Height, Depth ), e3 ) *
  160.                roty( -90 ),
  161.            coerce( planeSrf( 0, 0, Height, Depth ), e3 ) *
  162.                roty( -90 ) * trans( vector( Width, 0, 0 ) ) );
  163.  
  164. #
  165. # Transformations.
  166. #
  167. tx = function( r ):
  168.     return = trans( vector( r, 0, 0 ) );
  169. ty = function( r ):
  170.     return = trans( vector( 0, r, 0 ) );
  171. tz = function( r ):
  172.     return = trans( vector( 0, 0, r ) );
  173. sx = function( r ):
  174.     return = scale( vector( r, 1, 1 ) );
  175. sy = function( r ):
  176.     return = scale( vector( 1, r, 1 ) );
  177. sz = function( r ):
  178.     return = scale( vector( 1, 1, r ) );
  179. sc = function( s ):
  180.     return = scale( vector( s, s, s ) );
  181. rx = function( r ):
  182.     return = rotx( r );
  183. ry = function( r ):
  184.     return = roty( r );
  185. rz = function( r ):
  186.     return = rotz( r );
  187.  
  188. RotVec2Z = function( w ):u:v: # Rotation Trans. of w dir to Z axis.
  189.     if ( abs( coord( w, 0 ) ) > abs( coord( w, 1 ) ),
  190.      u = vector( 0, 1, 0 ),
  191.      u = vector( 1, 0, 0 ) ):
  192.     w = normalize( w ):
  193.     v = normalize( u ^ w ):
  194.     u = normalize( w ^ v ):
  195.     return =
  196.     homomat( list( list( coord( u, 0 ), coord( v, 0 ), coord( w, 0 ), 0 ),
  197.                list( coord( u, 1 ), coord( v, 1 ), coord( w, 1 ), 0 ),
  198.                list( coord( u, 2 ), coord( v, 2 ), coord( w, 2 ), 0 ),
  199.                list( 0, 0, 0, 1 ) ) );
  200. RotZ2Vec = function( w ): # Rotation Trans. of Z axis to W dir.
  201.     return = RotVec2Z( w )^-1;
  202.  
  203. #
  204. # Arrows.
  205. #
  206. arrow3d = function( Pt, Dir, Length, Width, HeadLength, HeadWidth ):
  207.     return = list( cylinSrf( Length - HeadLength, Width / 2 )
  208.             * RotZ2Vec( Dir )
  209.             * trans( Pt ),
  210.            coneSrf( HeadLength, HeadWidth )
  211.             * trans( vector( 0, 0, Length - HeadLength ) )
  212.             * RotZ2Vec( Dir )
  213.             * trans( Pt ) );
  214.  
  215. #
  216. # Emulation of view, interact and other useful viewing functions using VIEWOBJ
  217. # for the default display device.
  218. #
  219. beep = procedure():command_: # Make some noise.
  220.     command_ = "BEEP":
  221.     viewobj(command_);
  222. viewclear = procedure():command_: # Clear the screen.
  223.     command_ = "CLEAR":
  224.     viewobj(command_);
  225. viewdclear = procedure():command_: # Delayed clear screen.
  226.     command_ = "DCLEAR":
  227.     viewobj(command_);
  228. viewdisc = procedure():command_: # Disconnect from this display device
  229.     command_ = "DISCONNECT":
  230.     viewobj(command_);
  231. viewexit = procedure():command_: # Force Display device to exit.
  232.     command_ = "EXIT":
  233.     viewobj(command_);
  234. viewmsave = procedure(name):command_: # Save viewing matrix under name.
  235.     command_ = "MSAVE " + name:
  236.     viewobj(command_);
  237. viewremove = procedure(name):command_: # Remove an object from display.
  238.     command_ = "REMOVE " + name:
  239.     viewobj(command_);
  240. viewanim = procedure(TMin, TMax, Dt):command_: # Animate a sequence.
  241.     command_ = "ANIMATE " ^ TMin ^ " " ^ TMax ^ " " ^ Dt:
  242.     viewobj(command_);
  243. viewstate = procedure(state):command_: # Change state of display device.
  244.     command_ = "STATE " + state:
  245.     viewobj(command_);
  246. view = procedure(none,clear): # Emulation of old VIEW command.
  247.     if (clear != 0.0, viewdclear()):
  248.     viewobj(none);
  249. interact = procedure(none): # Emulation of old INTERACT command.
  250.     viewdclear():
  251.     viewobj(none):
  252.     pause();
  253.  
  254. #
  255. # Client communication helper functions.
  256. #
  257. clntclear = procedure(h):command_: # Clear the screen.
  258.     command_ = "CLEAR":
  259.     clntwrite(h, command_);
  260. clntdclear = procedure(h):command_: # Delayed clear screen.
  261.     command_ = "DCLEAR":
  262.     clntwrite(h, command_);
  263. clntdisc = procedure(h):command_: # Disconnect from this display device
  264.     command_ = "DISCONNECT":
  265.     clntwrite(h, command_);
  266. clntexit = procedure(h):command_: # Force Display device to exit.
  267.     command_ = "EXIT":
  268.     clntwrite(h, command_);
  269. clntgetobj = function(h, Name):command_: # Get an object from a client.
  270.     command_ = "GETOBJ " + Name:
  271.     clntwrite(h, command_):
  272.     return = clntread(h, 100);
  273. clntmsave = procedure(h, name):command_: # Save viewing matrix under name.
  274.     command_ = "MSAVE " + name:
  275.     clntwrite(h, command_);
  276. clntremove = procedure(h, name):command_: # Remove an object from display.
  277.     command_ = "REMOVE " + name:
  278.     clntwrite(h, command_);
  279. clntanim = procedure(h, TMin, TMax, Dt):command_: # Animate a sequence.
  280.     command_ = "ANIMATE " ^ TMin ^ " " ^ TMax ^ " " ^ Dt:
  281.     clntwrite(h, command_);
  282. clntstate = procedure(h, state):command_: # Change state of display device.
  283.     command_ = "STATE " + state:
  284.     clntwrite(h, command_);
  285. clntview = procedure(h, none, clear): # Emulation of old VIEW command.
  286.     if (clear != 0.0, clntclear(h)):
  287.     clntwrite(h, none);
  288. cntrintr = procedure(none): # Emulation of old INTERACT command.
  289.     clntclear(h):
  290.     clntwrite(h, none):
  291.     pause();
  292.  
  293. iritState("EchoSource", true);
  294.